package com.zsx.shopping.cart.converter;

import com.zsx.shopping.common.api.cart.vo.CartItemVO;
import com.zsx.shopping.common.api.product.vo.SaleAttrVO;
import com.zsx.shopping.common.api.product.vo.SkuVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.List;

@Mapper(componentModel = "spring")
public interface CartItemConverter {

    @Mappings({
            @Mapping(target = "skuId", source = "skuVO.id"),
            @Mapping(target = "title", source = "skuVO.name"),
            @Mapping(target = "image", source = "skuVO.defaultImage"),
            @Mapping(target = "saleAttrs", qualifiedByName = "saleAttrsMapping")
    })
    CartItemVO toCartItemVo(SkuVO skuVO, Integer count);

    @Named("saleAttrsMapping")
    default String saleAttrsMapping(List<SaleAttrVO> saleAttrVOs) {
        StringBuilder stringBuilder = new StringBuilder();
        for (SaleAttrVO saleAttrVO : saleAttrVOs) {
            stringBuilder.append(saleAttrVO.getAttrName()).append("：").append(saleAttrVO.getAttrValue()).append(",");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }
}
